package com.shen.manager;

import com.shen.model.Member;
import com.shen.util.RandomUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Author:shen
 * CreateDate:2020/6/2.
 * Function:
 */
public class Manager {
    /**
     * 初始化数据
     */
    static RandomUtils randomUtils=new RandomUtils();//随机生成卡号
    static Map<Integer, Member> memberMap=new HashMap<Integer, Member>();
    static {
        memberMap.put(98089088,new Member("小明",98089088,"123",1000,new Date()));
    }

    /**
     * 开户
     */
    public Member openAccount(Member member){
        int id = randomUtils.randomId(100000000, 9999999);
        member.setCardNum(id);
        member.setIntegral(100);//初始积分为100
        member.setDate(new Date());
        memberMap.put(id,member);
        return member;
    }
    /**
     * 通过会员号来查找会员
     */
    public Member getMember(Integer id){
      return  memberMap.get(id);
    }
    /**
     * 按注册名和密码判断用户是否存在
     */
    public Member getMemberByNameAndPWD(Integer id,String password){
        Member member=null;
        Member checkUser= getMember(id);
        if(checkUser!=null){
            boolean checkPasswod = checkUser.getPassword().equals(password);
            if(checkPasswod==true){
                member=checkUser;
            }
        }
        return member;
    }
    /**
     * 修改密码
     */
    public void changePassword(Member member){
        memberMap.get(member.getCardNum()).setPassword(member.getPassword());
    }
    /**
     *积分抵用
     */
    public boolean useIntegral(Member member,int integral){
        int useIntegral=member.getIntegral()-integral;
        if(useIntegral<0){
            return false;
        }else{
            memberMap.get(member.getCardNum()).setIntegral(useIntegral);
            return true;
        }
    }


}
